package com.example.dzbsh.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.Writer;
import java.util.Map;

public class DocUtil {
	
	private Logger log = LoggerFactory.getLogger(getClass());

	private Configuration configure = null;
	
	public DocUtil(){
		configure = new Configuration();
		configure.setDefaultEncoding("utf-8");
	}
	
	private Template getTemplate(String templatepath,String templatename){
		try {
			configure.setDirectoryForTemplateLoading(new File(templatepath));
			Template t = configure.getTemplate(templatename);
			t.setEncoding("utf-8");
			return t;
		} catch (Exception e) {
			throw new RuntimeException("getTemplate"+e.getMessage());
		}
	}
	
	public void write(String templatepath,String templatename,Map<String,Object> dataMap,Writer out){
		try {
			Template t = getTemplate(templatepath,templatename);
			t.process(dataMap, out);
		   	out.close();
		} catch (Exception e) {
			throw new RuntimeException("write"+e.getMessage());
		}
	}
}
